import { Suspense, lazy } from 'react'
import { Spin } from 'antd'
// createBrowserRouter
import { createHashRouter, Navigate } from "react-router";

import AuthRoute from '@/components/AuthRoute.jsx'
import Login from '@/pages/login/index.jsx'
import Test from '@/pages/test/index.jsx'
import User from '@/pages/user/index.jsx'
import Role from '@/pages/role/index.jsx'

// 全局路由
function globalRoute() {
const Layout = lazy(() => import('@/pages/layout'))
  return createHashRouter([
    {
      path: "/",
      element: (
        <AuthRoute>
          {/* 二级路由框架页面采用懒加载方式 */}
          <Suspense fallback={<Spin />}>
              <Layout></Layout>
          </Suspense>
        </AuthRoute>
      ),
      children: [
        // 默认路由-方式1
        // { index: true, element: <Navigate to="/user" replace /> }, // 新增：默认重定向到/user
        // 默认路由-方式2
        { path: "/", element: <Navigate to="/user" /> },
        { path: "user", element: <User /> },
        { path: "role", element: <Role /> },
        // 未匹配，跳转Login页面
        { path: "*", element: <Navigate to="/login" /> },
      ]
    },
    {
      path: "/login",
      element: (<Login />),
    },
    {
      path: "/test",
      element: (<Test />),
    },
  ])
}

const globalRouter = globalRoute()
export default globalRouter
